package com.rem.springIocAnnotation.controller;

import com.rem.springIocAnnotation.entity.User;
import com.rem.springIocAnnotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

/**
 * @author Rem
 * @date 2023-02-28
 */
@Controller
public class UserController {

    /**
     * 参数类型自动注入
     */
    //@Autowired
    //private UserService userService;

    /**
     * set方法注入
     */
//    private UserService userService;
//    @Autowired
//    public void setUserService(UserService userService) {
//        this.userService = userService;
//    }

    /**
     * 构造方法注入 当参数只有一个时，@Autowired注解可以省略
     */
    /*private UserService userService;

    //@Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }*/


    /**
     * 当有两个一样的类型类，注入会失败，因为无法识别它的子类，可以根据@Qualifier 指定具体类
     */
    @Autowired
    @Qualifier("userServiceImpl2")
    private UserService userService;

    public void saveUser() {
        userService.saveUser(new User(12, "kimi"));
    }
}
